#################### (you must change this to your group number)
GROUP_NUMBER := 03
####################

ERLC := erlc
ERLC_FLAGS := -W -I include

NIT := bin/nitrogen console

ERL_FILES := $(wildcard src/*.erl)
BEAM_FILES := $(patsubst src/%.erl,ebin/%.beam,${ERL_FILES})

comma:= ,
empty:=
space:= $(empty) $(empty)

EDOC_SRC := $(filter-out %_test.erl, $(ERL_FILES))
EDOC_SRC_LIST := [$(subst $(space),$(comma),$(patsubst src/%.erl,'src/%.erl', $(EDOC_SRC)))]

REQUIRED_DIR_NAME := pop_2011_group_$(GROUP_NUMBER)
PROJECT_DIR := $(notdir $(shell pwd))
ARCHIVE_NAME :=  $(REQUIRED_DIR_NAME)_archive__$(shell date "+%Y-%m-%d__%H:%M:%S")__.tar.gz
ARCHIVE_DIR := ..

all: $(BEAM_FILES)

ebin/%.beam: src/%.erl
	$(ERLC) $(ERLC_FLAGS) -o ebin $<

start: all
	(cd ebin && erl -name pronection_server -setcookie 1234567890 -eval 'serverCheck:start()')

start_web: 
	(cd src/nitrogen && $(NIT))

test: all
	(cd ebin && erl -noinput -eval 'eunit:test({dir, "."}, [verbose]), init:stop()')

doc: $(BEAM_FILES)
	erl -noshell -eval "edoc:files($(EDOC_SRC_LIST), [{dir, 'doc/html'}])" -s init stop

clean:
	rm -fr .#* *.dump
	rm -fr ebin/*.beam
	(cd doc/html && find . -name "*" -a ! -name overview.edoc -exec rm -rf {} \;)

remove_finderinfo:
	-xattr -d "com.apple.FinderInfo" src/*.erl include/*.hrl doc/* doc/html/*

archive: clean
ifeq ($(REQUIRED_DIR_NAME), $(PROJECT_DIR))
	(cd $(ARCHIVE_DIR) && tar cvfz $(ARCHIVE_NAME) $(PROJECT_DIR) )
	@echo 
	@echo NOTE: Archive created in $(ARCHIVE_DIR)/$(ARCHIVE_NAME)
	@echo 
else
	@echo Error: Wrong directory name $(PROJECT_DIR), change to $(REQUIRED_DIR_NAME)
endif

